package Util::UserTuan::UserTuanUpdate;
use Moose;
use namespace::autoclean;
use JSON;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

sub find_user_tuan {
	my ($self, $tuan_id, $user_id) = @_;

	return $self->schema->resultset('UserTuan')->search({tuan_id => $tuan_id, user_id => $user_id})->first();
}

sub tuan_delete_user {
	my ($self, $tuan_id, $user_id) = @_;

	my $user_tuan = $self->find_user_tuan($tuan_id, $user_id);
	$user_tuan->delete;
}

sub join_tuan {
	my ($self, $tuan_id, $user_id, $password) = @_;

	my $tuan = $self->schema->resultset('UserTuan')->create({
		tuan_id => $tuan_id, 
		user_id => $user_id,
		role => 'J',
		password => $password});
	return $tuan;
}

sub join_tuan_new {
	my ($self, $tuan_id, $user_id, $password, $user_name, $user_address, $cellphone, $university_id, $status, $join_tuan_comment) = @_;

	my $tuan = $self->schema->resultset('UserTuan')->create({
		tuan_id => $tuan_id, 
		user_id => $user_id,
		role => 'J',
		password => $password,
		user_name => $user_name,
		user_address => $user_address,
		user_cellphone => $cellphone,
		university_id => $university_id,
		jt_comment => $join_tuan_comment,
		status => $status}
		);
	return $tuan;
}

#sub tuan_new_to_done {
#	my ($self, $tuan_id, $user_id, $deal_quantity) = @_;
#
#	my $user_tuan = $self->find_user_tuan($tuan_id, $user_id);
#	#my $curr_time = `date +%F`;
#	my $curr_time = `date --iso-8601=seconds`;
#        $user_tuan->update({status=> "done", deal_quantity => $deal_quantity, deal_time => $curr_time});
#}
#
#sub tuan_done_to_new {
#	my ($self, $tuan_id, $user_id, $deposit_quantity) = @_;
#
#	my $user_tuan = $self->find_user_tuan($tuan_id, $user_id);
#        $user_tuan->update({status=> "join", deal_quantity => undef, deal_time => undef});
#}

#sub tuan_deposit_to_done {
#	my ($self, $tuan_id, $user_id, $deal_quantity) = @_;
#
#	#my $curr_time = `date +%F`;
#	my $curr_time = `date --iso-8601=seconds`;
#	my $user_tuan = $self->find_user_tuan($tuan_id, $user_id);
#        $user_tuan->update({status=> "done", deal_quantity => $deal_quantity, deal_time => $curr_time});
#}
#
#sub tuan_deposit_to_new {
#	my ($self, $tuan_id, $user_id) = @_;
#
#	my $user_tuan = $self->find_user_tuan($tuan_id, $user_id);
#        $user_tuan->update({status=> "join", deposit_quantity => undef, deposit_time => undef});
#}
#
#sub tuan_done_to_new {
#	my ($self, $tuan_id, $user_id) = @_;
#
#	my $user_tuan = $self->find_user_tuan($tuan_id, $user_id);
#	$user_tuan->update({status=> "join", deposit_quantity => undef, deposit_time => undef, deal_quantity => undef, deal_time => undef});
#}


__PACKAGE__->meta->make_immutable;

1;
